/**
 * async、await
 * async定义的函数是异步函数，它返回一个状态为fulfilled的promise，如果该函数有返回值，promise的结果就是该返回值
 * 用try...catch来捕获await的错误信息
 * await 后接 Promise 时自动解包值，非 Promise 则直接返回
 * 
*/



async function test(){
    console.log('1')
    return 'abc'
}
console.log(test())
console.log('2')
console.log('3')
//// 1 2 3
console.log('------------------------------------------------')

async function test(){
    await 1
    console.log('1')
}
test()
console.log('2')
console.log('3')
////2 3 1

console.log('------------------------------------------------')


async function test1() {
    console.log('test1')
    return new Promise((resolve, reject) => {
        resolve('resolve---test1')
    })
}
async function test2() {
    console.log('test2')
    return new Promise((resolve, reject) => {
        reject('resolve---test2')
    })
}

async function testOnly() {
    try {
        const res = await test1()
        console.log(res, 'res')
        await test2()

    } catch (err) {
        console.log(err, 'err')
    }
}
testOnly()
console.log('testOnly')

//test1  testOnly  resolve---test1 test2 resolve---test2

console.log('------------------------------------------------')

async function testFn(){
    return 'hello world'
}
testFn().then(value=>{
    console.log(value)   //hello world
})

console.log('------------------------------------------------')






